## @file
#  @author Chelsey Batchelor <vadedeyes@gmail.com>
#  @author Abraham Escamilla <abraham.escamilla@live.com>
#  @author Curtis Hollibaugh <curtis.hollibaugh@gmail.com>
#  @copyright Copyright 2014 Chelsey Batchelor <vadedeyes@gmail.com>
#  @copyright Copyright 2014 Abraham Escamilla <abraham.escamilla@live.com>
#  @copyright Copyright 2014 Curtis Hollibaugh <curtis.hollibaugh@gmail.com>
#  Main build script for the program.

# Helper functions

## Replaces originalDir with variantDir in path.
#  @param path        File path string.
#  @param originalDir Original directory that path starts with.
#  @param variantDir  Directory to replace originalDir with.
#  @return String containing the variant version of path.
def convertPathToVariant(path, originalDir, variantDir):
	if path.startswith(originalDir):
		temp = path.lstrip(originalDir)
		temp = variantDir + temp
		print(temp)
		return temp
	else:
		return path

## Replaces originalDir with variantDir in all paths in the list paths.
#  @param paths       List of file path strings.
#  @param originalDir Original directory that each path starts with.
#  @param variantDir  Directory to replace originalDir with.
#  @return List of strings containing the variant versions of the paths.
def convertPathsToVariants(paths, originalDir, variantDir):
	temp = []
	for path in paths:
		temp.append(convertPathToVariant(path, originalDir, variantDir))
	return temp



# Program sources
progSources = ['src/main.cpp', 'src/gui/gtk.cpp', 'src/gui/gui.cpp', 'src/gui/trayicon.cpp']
testSources = ['src/tests/foobar.cpp', 'src/tests/learn_gtk.cpp', 'src/tests/gui.cpp', 'src/gui/gui.cpp', 'src/gui/gtk.cpp', 'src/gui/trayicon.cpp', 'src/tests/trayicon.cpp']



# Build settings
# Global build settings
basicCCFLAGS = ['-Wall', '-Weffc++', '-Wextra', '-std=c++11']

# Release build settings
releaseCCFLAGS = basicCCFLAGS + ['-O3'] + ['`pkg-config', '--cflags', 'gtk+-3.0`']
releaseLINKFLAGS = ['`pkg-config', '--libs', 'gtk+-3.0`']

# Debug build settings
debugCCFLAGS = basicCCFLAGS + ['-g'] + ['`pkg-config', '--cflags', 'gtk+-3.0`']
debugLINKFLAGS = ['`pkg-config', '--libs', 'gtk+-3.0`']

# Test build settings
testCCFLAGS   = debugCCFLAGS + ['`pkg-config', '--cflags', 'gtk+-3.0`']
testLIBS      = ['gtest', 'gtest_main']
testLINKFLAGS = ['`pkg-config', '--libs', 'gtk+-3.0`']



# Path Settings
# Executable paths
releaseProgPath = 'bin/release/server'
debugProgPath   = 'bin/debug/server'
testProgPath    = 'bin/debug/tests'

# Variant paths
sourceDir         = 'src'
releaseVariantDir = 'tmp/release'
debugVariantDir   = 'tmp/debug'
testVariantDir    = 'tmp/test'
releaseProgSources = convertPathsToVariants(progSources, sourceDir, releaseVariantDir)
debugProgSources   = convertPathsToVariants(progSources, sourceDir, debugVariantDir)
testProgSources    = convertPathsToVariants(testSources, sourceDir, testVariantDir)



# Set up build environments
# Set up release environment
releaseEnv = Environment()
releaseEnv.VariantDir(releaseVariantDir, sourceDir)

# Set up debug environment
debugEnv = releaseEnv.Clone()
debugEnv.VariantDir(debugVariantDir, sourceDir)

# Set up testing environment
testEnv = debugEnv.Clone()
testEnv.VariantDir(testVariantDir, sourceDir)



# Build programs
# Build release program
releaseProg = releaseEnv.Program(target = releaseProgPath, source = releaseProgSources, CCFLAGS = releaseCCFLAGS, LINKFLAGS = releaseLINKFLAGS)
Default(releaseProg)
Alias('releaseProg', releaseProg)

# Build debug program
debugProg = debugEnv.Program(target = debugProgPath, source = debugProgSources, CCFLAGS = debugCCFLAGS, LINKFLAGS = debugLINKFLAGS)
Alias('debugProg', debugProg)

# Build test program
testProg = debugEnv.Program(target = testProgPath, source = testProgSources, CCFLAGS = testCCFLAGS, LIBS = testLIBS, LINKFLAGS = testLINKFLAGS)
Alias('testProg', testProg)
